//
// Package common
// @Author: Zhenwei Xiao
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/20 3:44 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const os = require('os');

//
// Home returns the home directory for the executing user
//
// @Description:
// @return string, error
//
function Home() {
    let user = os.userInfo()
    if  (user.homedir != null) {
        return [user.homedir, null]
    }

    // cross compile support
    if ("windows" === os.type()) {
        return homeWindows()
    }

    // Unix-like system, so just assume Unix
    return homeUnix()
}

//
// homeUnix returns the home directory for the executing user using Unix
//
// @Description:
// @return string, error
//
function homeUnix() {
    // First prefer the HOME environmental variable
    let home = process.env.HOME || process.env.USERPROFILE
    if (home != null) {
        return [home, null]
    }else if(home === null || home === "") {
        return ["", new Error("blank output when reading home directory")]
    }
}

//
// homeUnix returns the home directory for the executing user using Unix
//
// @Description:
// @return string, error
//
function homeWindows() {
    let home = process.env.HOME || process.env.USERPROFILE

    if (home === null) {
        return ["", new Error("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")]
    }

    return [home, null]
}

module.exports = {
    Home: Home,
}